﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FA.Library.Sim.Aircraft;
using FA.Library.Sim.Events;

namespace FA.Plugins.GUIPlugin.ViewModel
{
    public class MainWindowVM : ViewModel
    {
        public event SimAircraftModelChangedEvent OnSimAircraftModelChangedEvent;

        public AircraftModel Aircraft
        {
            get { return _aircraftModel; }
            private set
            {
                if (_aircraftModel != value)
                {
                    lock (_modelLock)
                    {
                        _aircraftModel = value;

                        // Notify the UI
                        NotifyPropertyChanged("Aircraft");

                        // Notify the sim listeners
                        if (OnSimAircraftModelChangedEvent != null && !_supressModelChangedEvent)
                            OnSimAircraftModelChangedEvent(this, new SimAircraftModelChangedEventArgs((AircraftModel)Aircraft.Clone()));
                    }
                }
            }
        }
        private AircraftModel _aircraftModel;

        private bool _supressModelChangedEvent = false;
        private readonly object _modelLock = new object();

        /// <summary>
        /// Update the aircraft model for the plugin.
        /// </summary>
        /// <param name="model"></param>
        public void UpdateAircraftModel(AircraftModel model, bool supressEvent)
        {
            lock (_modelLock)
            {
                _supressModelChangedEvent = supressEvent;

                Aircraft = model;

                _supressModelChangedEvent = false;
            }
        }
    }
}
